﻿// -----------------------------------------------------------------------
// Copyright (c) David Kean. All rights reserved.
// -----------------------------------------------------------------------
extern alias pcl;
using System;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using pcl::System;
using System.IO;
using System.Net;
using System.Threading;
using System.Xml;
using Moq;
using Moq.Protected;

namespace Portable
{
    [TestClass]
    public class CloseExtensionTests
    {
        [TestMethod]
        public void Close1_NullAsReader_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("reader", () =>
            {
                CloseExtensions.Close((BinaryReader)null);
            });
        }

        [TestMethod]
        public void Close1_ValueAsReader_CallsDispose()
        {
            int callCount = 0;
            var stream = StreamFactory.CreateReadableStream();

            var reader = IDisposableFactory.OverrideDispose<BinaryReader>(stream, (disposing) => {
              callCount++;
            });
            
            CloseExtensions.Close(reader);

            Assert.AreEqual(1, callCount);

        }

        [TestMethod]
        public void Close2_NullAsWriter_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("writer", () =>
            {
                CloseExtensions.Close((BinaryWriter)null);
            });
        }

        [TestMethod]
        public void Close1_ValueAsWriter_CallsDispose()
        {
            int callCount = 0;
            var stream = StreamFactory.CreateWritableStream();

            var writer = IDisposableFactory.OverrideDispose<BinaryWriter>(stream, (disposing) => {
                callCount++;
            });

            CloseExtensions.Close(writer);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Close3_NullAsStream_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("stream", () =>
            {
                CloseExtensions.Close((Stream)null);
            });
        }

        [TestMethod]
        public void Close3_ValueAsStream_CallsDispose()
        {
            int callCount = 0;
            var stream = IDisposableFactory.OverrideDispose<Stream>((disposing) => {
                callCount++;
            });

            CloseExtensions.Close(stream);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Close4_NullAsResponse_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("response", () =>
            {
                CloseExtensions.Close((WebResponse)null);
            });
        }

        [TestMethod]
        public void Close4_ValueAsResponse_CallsDispose()
        {
            int callCount = 0;
            
            var response = IDisposableFactory.OverrideDispose<WebResponse>((disposing) => {
                callCount++;
            });

            CloseExtensions.Close(response);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Close5_NullAsHandle_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("handle", () =>
            {
                CloseExtensions.Close((WaitHandle)null);
            });
        }

        [TestMethod]
        public void Close5_ValueAsHandle_CallsDispose()
        {
            int callCount = 0;

            var handle = IDisposableFactory.OverrideDispose<WaitHandle>((disposing) => {
                callCount++;
            });

            CloseExtensions.Close(handle);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Close6_NullAsReader_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("reader", () =>
            {
                CloseExtensions.Close((XmlReader)null);
            });
        }

        [TestMethod]
        public void Close6_ValueAsReader_CallsDispose()
        {
            int callCount = 0;

            var reader = IDisposableFactory.OverrideDispose<XmlReader>((disposing) => {
                callCount++;
            });

            CloseExtensions.Close(reader);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Close7_NullAsWriter_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("writer", () => {
                CloseExtensions.Close((XmlWriter)null);
            });
        }

        [TestMethod]
        public void Close7_ValueAsWriter_CallsDispose()
        {
            int callCount = 0;

            var writer = IDisposableFactory.OverrideDispose<XmlWriter>((disposing) => {
                callCount++;
            });

            CloseExtensions.Close(writer);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Close8_NullAsReader_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("reader", () =>
            {
                CloseExtensions.Close((TextReader)null);
            });
        }

        [TestMethod]
        public void Close8_ValueAsReader_CallsDispose()
        {
            int callCount = 0;

            var reader = IDisposableFactory.OverrideDispose<TextReader>((disposing) => {
                callCount++;
            });

            CloseExtensions.Close(reader);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Close9_NullAsWriter_ThrowsArgumentNull()
        {
            ExceptionAssert.ThrowsArgumentNull("writer", () =>
            {
                CloseExtensions.Close((TextWriter)null);
            });
        }

        [TestMethod]
        public void Close9_ValueAsWriter_CallsDispose()
        {
            int callCount = 0;

            var writer = IDisposableFactory.OverrideDispose<TextWriter>((disposing) => {
                callCount++;
            });

            CloseExtensions.Close(writer);

            Assert.AreEqual(1, callCount);
        }
    }
}
